#ifndef TINI_H
#define TINI_H

#include <Windows.h>
#include "tlist.h"

/*
 *	
 */
class TIniKey : public TListObj
{
public:
	TIniKey( const char* key = NULL, const char* value = NULL )
		: TListObj(){ Set( key, value ); }
	~TIniKey(){ free(_key); free(_value); }

	void Set( const char* key, const char* value )
	{
		if( key ){ free( _key ); _key = _strdup( key ); }
		if( value ) { free( _value ); _value = _strdup( key ); }
	}

	const char* GetKey() { return _key; }
	const char* GetValue() { return _value; }

protected:
	char*	_key;	// null means comment line
	char*	_value;
};

/*
 *	
 */
class TIniSection : public TListObj, public TList
{
public:
	TIniSection();
	~TIniSection();

	void Set( const char* name );
	TIniKey* SearchKey( const char* keyName );
	bool AddKey( const char* key, const char* value );
	bool DelKey( const char* key );
	const char* GetName(){ return _name; }

protected:
	char*	_name;
};

/*
 *	
 */
class TIniFile : public TList
{
public:
	TIniFile( const char* filename = NULL );
	~TIniFile();

	void Init( const char* fileName = NULL );
	void UnInit();

	void SetSection( const char* section );
	bool DelSection( const char* section );
	bool DelKey( const char* keyName );

	bool StartUpdate();
	bool EndUpdate();

	bool SetStr( const char* keyName, const char* value );
	DWORD GetStr( const char* keyName, char* value, int maxSize, const char* defaultValue = "" );
	bool SetInt( const char* keyName, int value );
	int  GetInt( const char* keyName, int defaultValue = -1 );

	const char* GetFileName(){ return _iniFileName; }

protected:
	BOOL Lock();
	void Unlock();

	TIniSection* SearchSection( const char *section );
	bool WriteIni();

protected:
	char*			_iniFileName;
	int				_iniSize;
	FILETIME		_iniFileTime;

	TIniSection*	_curSection;
	TIniSection*	_rootSectin;
	
	HANDLE			_hMutex;
};

#endif	// TINI_H